<template>
    <div class="advice">
        <div class="buttonAll">
            <el-button type="primary" size="mini" @click="groupNumber --; indexNumber = 1;">新组</el-button>
            <el-button :disabled="!(gcList.length && !gcList[0]['applyExecDoctorName'])" type="primary" size="mini" @click="groupAdd">组内插入</el-button>
            <el-button type="success" size="mini" :loading="submitLoading" @click="submit">保存</el-button>
            <el-button :disabled="!!gcList.length && (!(gcList.length && !gcList[0]['applyExecDoctorName'] && gcList[0]['adviceCode'] > 0))" type="success" size="mini" :loading="executeLoading" @click="execute">开始执行</el-button>
            <!-- <el-button :disabled="!(gcList.length && !gcList[0]['applyExecDoctorName'] && gcList[0]['adviceCode'] > 0)" type="success" size="mini" :loading="executeLoading" @click="execute">开始执行</el-button> -->
            <el-button :disabled="!(gcList.length && !gcList[0]['confirmExecNurseName'] && gcList[0]['applyExecDoctorName'])" type="warning" size="mini" :loading="removeLoading" @click="remove">撤销</el-button>
            <el-button :disabled="!(gcList.length && gcList[0]['confirmExecNurseName'] && !gcList[0]['applyTerminationDoctorName'])" type="warning" size="mini" :loading="stopLoading" @click="stop">停止</el-button>
            <el-button :disabled="!(gcList.length && !gcList[0]['applyExecDoctorName'])" type="danger" size="mini" :loading="delLoading" @click="del">删除</el-button>

            <el-button type="primary" size="mini" @click="prescriptionShow = true">读取处方</el-button>

            <el-button type="primary" size="mini" @click="print(1)">打印</el-button>



            <el-dialog
                append-to-body
                title="处方"
                :visible.sync="prescriptionShow"
                width="800px">
                <div class="tableBox">
                    <el-date-picker
                        style="margin-bottom: 20px;"
                        v-model="startAndFinish"
                        @change="getPrescription()"
                        type="daterange"
                        range-separator="至"
                        start-placeholder="处方开始日期"
                        end-placeholder="处方结束日期">
                    </el-date-picker>
                    <el-table v-loading="prescriptionLoading" ref="multipleTable" :data="templateList" border @selection-change="handleSelectionChange">
                        <el-table-column
                            type="selection"
                            width="55">
                        </el-table-column>
                        <el-table-column label="模板名称" min-width="160px">
                            <template scope="scope">
                                <span>{{ scope.row.title }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="模板内容" min-width="400px">
                            <template scope="scope">
                                <span v-for="item in scope.row.prescriptionList">{{ item.orderNo }}；</span>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="prescriptionShow = false">取消</el-button>
                    <el-button type="primary">确定</el-button>
                </span>
            </el-dialog>

        </div>

        <div ref="print" class="mytables">
            <table style="width: 100%" class="mytable" cellpadding="0" cellspacing="1">
                <thead>
                    <tr class="head">
                        <td colspan="16">
                            <h3>长期医嘱单</h3>
                            <div class="info">
                                <div class="item">姓名： <span>{{ pInfo.elderlyName }}</span></div>
                                <div class="item">床号： <span>{{ pInfo.checkInBed }}</span></div>
                                <div class="item">级别： <span>{{ pInfo.nursingLevelName }}</span></div>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <th>类</th>
                        <th>组</th>
                        <th>序</th>
                        <th>开始时间</th>
                        <th>医嘱项目</th>
                        <th>用量</th>
                        <th>用法</th>
                        <th>频率</th>
                        <th>备注</th>
                        <th>数量</th>
                        <th>本院</th>
                        <th>医师</th>
                        <th>护士</th>
                        <th>结束时间</th>
                        <th>结束医师</th>
                        <th>结束护士</th>
                    </tr>
                </thead>
                <tbody>
                    <template v-for="row in printData">
                        <tr v-for="(item, index) in row.items">
                            <td>{{ item.medicalAdviceType }}</td>
                            <td v-if="!index" :rowspan="row.items.length">{{ item.adviceCode }}</td>
                            <td>{{ item.orderNo }}</td>
                            <td v-if="!index" :rowspan="row.items.length">{{ item.applyExecTime | dateTime }}</td>
                            <td>{{ item.item }}</td>
                            <td>{{ item.dosage }}</td>
                            <td v-if="!index" :rowspan="row.items.length">{{ item.useage ? item.useage.split('_')[0] : '' }}</td>
                            <td v-if="!index" :rowspan="row.items.length">{{ item.frequency ? item.frequency.split('_')[0] : '' }}</td>
                            <td>{{ item.remark }}</td>
                            <td>{{ item.quantity }}</td>
                            <td>{{ item.medicalAdviceSource == 1 ? '∨' : '' }}</td>
                            <td>{{ item.applyExecDoctorName }}</td>
                            <td>{{ item.confirmExecNurseName }}</td>
                            <td>{{ item.applyTerminationTime | dateTime }}</td>
                            <td>{{ item.applyTerminationDoctorName }}</td>
                            <td>{{ item.confirmTerminationNurseName }}</td>
                        </tr>
                    </template>
                </tbody>
            </table>
        </div>


        <el-table :data="tableData" class="tb-edit" border style="width: 100%" :row-class-name="tableRowClassName" :span-method="objectSpanMethod">
            <el-table-column :key="item.label" :label="item.label" :min-width="item.width || ''" v-for="(item, index) in config.column">
                <template scope="scope">
                    <!-- 普通文本 -->
                    <span class="text" v-if="item.type == 'text'">{{ scope.row[item.key] }}
                        <el-checkbox :disabled="!scope.row.checkEdit" @change="groupCheckedChange(scope.row[item.key], $event)" v-if="item.key == 'adviceCode' && scope.row[item.key]" :label="scope.row[item.key]" v-model="groupChecked"></el-checkbox>
                    </span>
                    <!-- 日期文本 -->
                    <span class="text" v-if="item.type == 'dateText'">{{ scope.row[item.key] | dateTime('yyyy-MM-dd') }}</span>
                    <!-- 日期时间文本 -->
                    <span class="text" v-if="item.type == 'dateTimeText'">{{ scope.row[item.key] | dateTime }}</span>
                    <!-- 输入框 -->
                    <el-input size="mini" :ref="setRef(scope, index, item)" v-if="item.type == 'input'" :disabled="!scope.row.edit || item.disabled" v-model="scope.row[item.key]" @change="nextFocus(scope.$index, index, 0, 1)" @keyup.enter.native="nextFocus(scope.$index, index)"></el-input>
                    <!-- 数字输入框 -->
                    <el-input-number size="mini" :class="{ 'max': (item.max >= 0 ? item.max : (scope.row[item.max] ? Number(scope.row[item.max]) : 999999999)) == scope.row[item.key] }" :disabled="!scope.row.edit || item.disabled" :precision="item.precision >= 0 ? item.precision : 2" :ref="setRef(scope, index, item)" v-if="item.type == 'number'" :controls="false" v-model="scope.row[item.key]" :min="item.min || 0" :max="item.max >= 0 ? item.max : (scope.row[item.max] ? Number(scope.row[item.max]) : 999999999)" @change="nextFocus(scope.$index, index, 0, 1)" @keyup.enter.native="nextFocus(scope.$index, index)"></el-input-number>
                    <!-- 复选框 -->
                    <el-checkbox :class="{ 'opacity': !scope.row.medicalAdviceType }" :disabled="!scope.row.edit" :true-label="1" :false-label="2" v-if="item.type == 'check'" v-model="scope.row[item.key]" @change="nextFocus(scope.$index, index, 0, 5, scope.row)"></el-checkbox>
                    <!-- 下拉选择 -->
                    <el-select :filterable="item.filterable" size="mini" :ref="setRef(scope, index, item)" popper-class="preSelect" :disabled="!scope.row.edit || item.disabled" placeholder="" v-if="item.type == 'select'" v-model="scope.row[item.key]" @change="nextFocus(scope.$index, index, 150, 2, scope.row)">
                        <template v-if="Array.isArray(item.options)">
                            <el-option v-for="opt in item.options" :key="opt.value" :label="opt.label" :value="opt.value">
                            </el-option>
                        </template>
                        <template v-else>
                            <el-option v-for="opt in scope.row[item.key + 'Options']" :key="opt[item.options.value]" :label="opt[item.options.label]" :value="opt[item.options.value]">
                            </el-option>
                        </template>
                    </el-select>
                    <!-- 下拉选择 -->
                    <el-select :filterable="item.filterable" size="mini" :ref="setRef(scope, index, item)" popper-class="preSelect" :disabled="!scope.row.edit || item.disabled" placeholder="" v-if="item.type == 'selectInput'" v-model="scope.row[item.key]" @change="nextFocus(scope.$index, index, 0, 6, scope.row)" @keyup.enter.native="nextFocus(scope.$index, index, 150, 4)">
                        <template v-if="Array.isArray(item.options)">
                            <el-option v-for="opt in item.options" :key="opt.value" :label="opt[item.opLabel]" :value="opt[item.opValue]">
                            </el-option>
                        </template>
                        <template v-else>
                            <el-option v-for="opt in scope.row[item.key + 'Options']" :key="opt[item.options.value]" :label="opt[item.options.label]" :value="opt[item.options.value]">
                            </el-option>
                        </template>
                    </el-select>
                    <!-- 日期输入框 -->
                    <el-date-picker size="mini" :disabled="!scope.row.edit || item.disabled" popper-class="inputDate" format="yyyyMMdd" :ref="setRef(scope, index, item)" v-if="item.type == 'inputDate'" v-model="scope.row[item.key]" type="date" @change="nextFocus(scope.$index, index, 150)" :pickerOptions="pickerOptions"></el-date-picker>
                    <!-- 日期选择框 -->
                    <el-date-picker size="mini" :disabled="!scope.row.edit || item.disabled" :ref="setRef(scope, index, item)" v-if="item.type == 'date'" v-model="scope.row[item.key]" type="date" @change="nextFocus(scope.$index, index, 150)"></el-date-picker>
                    <!-- 日期选择框 -->
                    <el-date-picker size="mini" :disabled="!(scope.row.edit && scope.row.medicalAdviceType)" :ref="setRef(scope, index, item)" v-if="item.type == 'datetime'" v-model="scope.row[item.key]" type="datetime" @change="nextFocus(scope.$index, index, 150, 3, scope.row)"></el-date-picker>
                    <!-- 乘积 -->
                    <span class="text" v-if="item.type == 'multiply'">{{ scope.row[item.key] = numberIsNaN((scope.row[item.m1] * scope.row[item.m2])) | money }}</span>
                    <!-- 减去 -->
                    <span class="text" v-if="item.type == 'subtract'">{{ scope.row[item.key] = numberIsNaN((scope.row[item.m1] - scope.row[item.m2])) | money }}</span>
                </template>
            </el-table-column>
            <el-table-column v-if="!this.config.hiddenDel" label="操作" min-width="50px" :fixed="this.fixed ? 'right' : false" align="center">
                <template scope="scope">
                    <el-button v-if="scope.row.edit && scope.row.medicalAdviceType && scope.row.adviceCode < 0" type="text" @click="handleDelete(scope.$index)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="btnAll" v-if="config.group">
            <el-button @click="grouping">分组</el-button>
            <el-button @click="ungroup" type="text">取消分组</el-button>
        </div>
    </div>
</template>

<script>
    let colorIndex = true
    import request from '@/utils/request'
    import { dateTime, mapLoction } from "@/utils/index"
    import { medicalSave, medicalApplyterm, medicalConfirmexec, medicalConfirmterm, medicalDelete, medicalDoctorexec, medicalRevocation } from "@/api/advice"
    import Print from '@/utils/print'
    import Vue from 'vue'
    Vue.use(Print)
    export default {
        name: "editTable",
        components: {  },
        filters:{
            dateTime
        },
        props: {
            value: {
                type: Array,
                default: ''
            },
            config: {
                type: Object,
                default: {}
            },
            fixed: {
                type: Boolean,
                default: false
            },
            dynamic: {
                type: Boolean,
                default: false
            },
            defaultLength: {
                type: Number,
                default: 20
            },
            height: {
                type: String,
                default: 'auto'
            },
            pInfo: {
                type: Object,
                default: {}
            }
        },
        computed:{
            startAndFinish:{
                get(){
                    return [this.queryForm.checkedInTimeStart, this.queryForm.checkedInTimeEnd]
                },
                set(val){
                    this.queryForm.checkedInTimeStart = val ? val[0] : ''
                    this.queryForm.checkedInTimeEnd = val ? val[1] : ''
                },
            },
        },
        data(){
            return {
                tableData: [],
                groupIndex: 0,
                FRI: 0,
                FII: 0,
                newLine: 0,
                pickerOptions: {
                    disabledDate(time) {
                        return time.getTime() < Date.now();
                    },
                },
                groupNumber: -1,
                indexNumber: 1,
                groupChecked: [],
                gcList: [],
                submitLoading: false,
                removeLoading: false,
                executeLoading: false,
                delLoading: false,
                stopLoading: false,
                rowColor: 1,
                prescriptionShow: false,
                prescriptionLoading: false,
                templateList: [],
                queryForm: {
                    checkedInTimeStart: '',
                    checkedInTimeEnd: '',
                },
                printData: [],
            }
        },
        mounted() {
            this.valueChange(this.value)
        },
        methods: {
            setList(){
                if(!this.config.noAdd){
                    let list = [...this.tableData]
                    let length = this.defaultLength - list.length
                    for(let i = 0; i < list.length; i++){
                        let item = this.tableData[i]
                        this.config.column.map(async(row) => {
                            if(row.type == 'select' && !Array.isArray(row.options) && !row[row.key + 'Options']){
                                this.$set(item, row.key + 'Options', await this.getOption(item, row.options))
                            }
                        })
                    }
                    for(let i = 0; i < length; i++){
                        let row = {...this.config.data}
                        list.push(row)
                    }
                    list[0].edit = true
                    this.tableData = list
                }
            },
            async getOption(row, options){
                let res = await request({
                    url: options.api.replace('@', row[options.query]),
                    method: options.method || 'get',
                    data: options.data || {},
                    params: options.data || {}
                })
                if(res.code == '200' && res.data){
                    if(res.data.curr){
                        if(res.data.data){
                            return res.data.data
                        }
                    }else{
                        return res.data
                    }
                }
            },
            searchChange(row, index){
                let ass = this.config.assignment
                if(ass){
                    ass.forEach(item => {
                        row[item.key1] = row[item.key2]
                    })
                }
                this.tableData[index] = Object.assign(this.tableData[index], row)
                this.config.column.map(async(row) => {
                    if(row.type == 'select' && !Array.isArray(row.options) && !row[row.key + 'Options']){
                        this.$set(this.tableData[index], row.key + 'Options', await this.getOption(this.tableData[index], row.options))
                    }
                })
                
            },
            handleDelete(index){
                this.tableData.splice(index, 1)
                // this.tableData.push({...this.config.data})
                this.setList()
            },
            checkArr(list = this.tableData){
                for(let i=0; i<list.length; i++){
                    let item = list[i]
                    // let sum = 0
                    // Object.keys(item).forEach(key => {
                    //     if(item[key]){
                    //         sum++
                    //     }
                    // })
                    if(item.medicalAdviceType){
                        if(!list[i + 1]){
                            list.push({...this.config.data})
                        }
                        list[i]['edit'] = true
                        list[i + 1]['edit'] = true

                        if(list[i]['applyExecDoctorName']){
                            list[i]['edit'] = false
                            if(list[i]['editDoctor'] == localStorage.userDoctorNumber && !list[i]['applyTerminationDoctorName']){
                                list[i]['checkEdit'] = true
                            }
                        }else if(list[i]['editDoctor'] == localStorage.userDoctorNumber || list[i]['adviceCode'] < 0){
                            list[i]['checkEdit'] = true
                        }
                        
                    }
                }
            },
            tableRowClassName({row, rowIndex}) {
                if(row.adviceCode){
                    if(!rowIndex || (rowIndex && this.tableData[rowIndex - 1].adviceCode != row.adviceCode)){
                        if(!rowIndex){
                            colorIndex = true
                        }else{
                            colorIndex = !colorIndex
                        }
                        return 'color' + colorIndex
                    }else{
                        return 'color' + colorIndex
                    }
                }
                
            },
            valueChange(list) {
                this.groupNumber = -1
                this.indexNumber = 1
                this.groupChecked = []
                this.tableData = list
                this.newLine = list.length > this.defaultLength ? list.length : this.defaultLength
                this.setList()
            },
            grouping(){
                let list = this.tableData.filter(item => item.applyExecTime)
                if(this.checkGroup(list)){
                    this.tableData.forEach(item => {
                        if(item.applyExecTime){
                            item.applyExecTime = false
                            item.a005 = this.groupIndex + 1
                        }
                    })
                    this.groupIndex ++
                }else{
                    this.$message.error('频率和用法不能为空，且组内需要一致')
                }
            },
            ungroup(){
                this.tableData.forEach(item => {
                    if(item.applyExecTime){
                        item.applyExecTime = false
                        item.a005 = ''
                    }
                })
            },
            checkGroup(a){
                for(let i=0; i<a.length; i++){
                    if(i){
                        if(!a[i].a008 || !a[i].a009 || a[i].a008 != a[i-1].a008 || a[i].a009 != a[i-1].a009){
                            return false
                        }
                    }
                }
                return true
            },
            nextFocus($index, index, ms = 0, type, row){
                this.FRI = $index
                this.FII = index  
                if(type == 2){
                    if(!row.adviceCode){
                        if($index){
                            if(row.medicalAdviceType != this.tableData[$index - 1].medicalAdviceType){
                                this.groupNumber --
                                this.indexNumber = 1
                            }
                        }
                        row['adviceCode'] = this.groupNumber
                        row['orderNo'] = this.indexNumber ++
                        if($index){
                            let oldRow = { ...this.tableData[$index - 1] }
                            if(oldRow.adviceCode == this.groupNumber){
                                row['applyExecTime'] = oldRow['applyExecTime']
                                row['medicalAdviceSource'] = oldRow['medicalAdviceSource']
                                row['useage'] = oldRow['useage']
                                row['frequency'] = oldRow['frequency']
                            }                
                        }
                    }else{
                        let list = this.tableData.filter(item => item.adviceCode == row.adviceCode)
                        list.forEach(item => {
                            item.medicalAdviceType = row.medicalAdviceType
                        })

                        // let gLength = 0
                        // this.tableData.forEach(item => {
                        //     if(item.medicalAdviceType){
                        //         if(item.adviceCode == row.adviceCode){
                        //             gLength ++
                        //             item.medicalAdviceType = row.medicalAdviceType
                        //         }
                        //     }
                        // })

                        if(list.length > 1){
                            this.$message.warning('请注意将会修改同组内所有的类型')
                        }
                    }
                }
                if(type == 3 || type == 5 || type == 6){
                    for(let i=$index + 1; i<this.tableData.length - 1; i++){
                        let item = this.tableData[i]
                        if(row.adviceCode == item.adviceCode){
                            item.applyExecTime = row.applyExecTime
                            item.medicalAdviceSource = row.medicalAdviceSource
                            item.useage = row.useage
                            item.frequency = row.frequency
                            // this.$set(item, 'changed', true)
                        }else{
                            break
                        }
                    }
                }
                if(type == 4){
                    let ref = 'input_' + $index + '_' + index
                    let doms = this.$refs[ref][0]
                    doms.blur()
                }
                setTimeout(() => {
                    if(type != 1 && type != 5 && type != 6){
                        let item = this.getNext()
                        console.log(item)
                        if(!item){
                            item = this.getNext()
                            if(!item){
                                item = this.getNext()
                            }
                        }
                        item && item.focus()
                        !item && this.$emit('last-next')
                    }
                }, ms)

                row = row || this.tableData[$index]
                let changedList = this.tableData.filter(item => item.adviceCode == row.adviceCode)
                changedList.forEach(item => {
                    this.$set(item, 'changed', true)
                })
            },
            getNext(){
                let x;
                if(this.fixed && !this.dynamic){
                    let sum = this.config.column.filter(item => !item.disabled)
                    // sum = sum.filter(item => !item.skip)
                    if(this.FRI == this.newLine - 1 && this.FII == sum.length - 1){
                        x = 0
                    }else{
                        x = this.FRI >= this.newLine ? 0 : 1
                    }
                }else{
                    x = 0
                }
                for(let i=this.FII + 1; i<this.config.column.length; i++){
                    this.FII = i
                    let ref = 'input_' + this.FRI + '_' + i
                    let doms = this.$refs[ref]
                    if(doms){
                        return doms[x]
                    }
                }
                this.FRI++
                this.FII = this.config.FII || 0
                let ref = 'input_' + this.FRI + '_' + this.FII
                let doms = this.$refs[ref]
                if(doms){
                    return doms[x]
                }
                return false
            },
            setRef(scope, index, item){
                if(!item.skip && !item.disabled){
                    return 'input_' + scope.$index + '_' + index
                }else{
                    return ''
                }
            },
            numberIsNaN(n){
                return isNaN(n) ? '' : n
            },
            objectSpanMethod({ row, column, rowIndex, columnIndex }) {
                if (columnIndex === 1 || columnIndex === 3 || columnIndex === 6 || columnIndex === 7 || columnIndex === 10) {
                    if(row.adviceCode){
                        if(!rowIndex || (rowIndex && this.tableData[rowIndex - 1].adviceCode != row.adviceCode)){
                            let rowspan = 0
                            for(let i=rowIndex; i<this.tableData.length - 1; i++){
                                let item = this.tableData[i]
                                if(row.adviceCode == item.adviceCode){
                                    rowspan++
                                }else{
                                    break
                                }
                            }
                            return {
                               rowspan,
                               colspan: 1
                            }
                        }else{
                            if(rowIndex){
                                return {
                                   rowspan: 0,
                                   colspan: 0
                                } 
                            }
                        }
                    }
                }
            },
            groupCheckedChange(val, type){
                this.groupChecked = type ? [val] : []
                this.gcList = type ? this.tableData.filter(item => val == item.adviceCode) : []
            },
            groupAdd(){
                if(this.groupChecked.length){
                    let id = this.groupChecked[0]
                    let lastIndex = 0
                    for(let i=0; i<this.tableData.length; i++){
                        let item = this.tableData[i]
                        if(item.adviceCode == id){
                            if(item.applyExecDoctorName){
                                this.$message.error('已开始执行的项目禁止插入')
                                return false
                            }
                            lastIndex = i
                        }
                    }
                    let oldRow = { ...this.tableData[lastIndex] }
                    let row = {...this.config.data}
                    row.medicalAdviceType = oldRow.medicalAdviceType
                    row.adviceCode = oldRow.adviceCode
                    row.orderNo = oldRow.orderNo + 1
                    row.applyExecTime = oldRow.applyExecTime
                    row.medicalAdviceSource = oldRow.medicalAdviceSource
                    row.useage = oldRow.useage
                    row.frequency = oldRow.frequency
                    this.tableData.splice(lastIndex + 1, 0, row)
                    this.groupChecked = []
                    this.gcList = []
                    this.nextFocus(lastIndex + 1, 0)

                }else{
                    this.$message.error('请选择组')
                }
            },
            submit(){
                this.submitLoading = true
                let list = this.tableData.filter(item => item.changed)
                let tree = mapLoction(list, 'adviceCode', 'items')
                tree.forEach(item => {
                    item.checkInMark = this.pInfo.checkInMark
                    item.institutionNumber = localStorage.institutionNumber
                    item.editDoctor = localStorage.userDoctorNumber
                    item.medicalAdviceSource = item.items[0].medicalAdviceSource
                    item.execDate = item.items[0].applyExecTime
                })
                medicalSave(tree).then(res => {
                    this.submitLoading = false
                    list.forEach(item => {
                        item.changed = false
                    })
                    this.$emit('change')
                }).catch(err => {
                    this.submitLoading = false
                })
            },
            remove(){
                if(this.groupChecked.length){
                    this.removeLoading = true
                    medicalRevocation({
                        adviceCode: this.groupChecked[0],
                        checkInMark: this.pInfo.checkInMark,
                        institutionNumber: localStorage.institutionNumber,
                    }).then(res => {
                        this.removeLoading = false
                        this.$emit('change')
                    }).catch(err => {
                        this.removeLoading = false
                    })
                }else{
                    this.$message.error('请选择组')
                }
            },
            execute(){
                const fn = (arr, list) => {
                    medicalDoctorexec({
                        adviceCodes: arr,
                        checkInMark: this.pInfo.checkInMark,
                        editDoctor: localStorage.userDoctorNumber,
                        execDate: list[0].applyExecTime || new Date(),
                        institutionNumber: localStorage.institutionNumber,
                    }).then(res => {
                        this.executeLoading = false
                        this.$emit('change')
                        this.groupChecked = []
                        this.gcList = []
                    }).catch(err => {
                        this.executeLoading = false
                    })
                }
                let noSubmitList = this.tableData.filter(item => item.changed)
                if(noSubmitList.length){
                    this.$message.error('存在未保存的项目，请先保存再执行')
                    return false
                }
                if(this.groupChecked.length){
                    let list = this.tableData.filter(item => this.groupChecked[0] == item.adviceCode)
                    this.executeLoading = true
                    fn(this.groupChecked, list)
                }else{
                    this.$alert('确定执行所有可执行项目？如需要单独执行请点击组选择', '提示', {
                        confirmButtonText: '确定执行',
                        callback: action => {
                            if(action == 'confirm'){
                                this.executeLoading = true
                                let list = this.tableData.filter(item => (!item.applyExecDoctorName && item.medicalAdviceType && item.editDoctor == localStorage.userDoctorNumber))

                                if(!list.length){
                                    this.$message.error('暂无可执行项目')
                                    this.executeLoading = false
                                    return false
                                }
                                let tree = mapLoction(list, 'adviceCode', 'items')
                                let res = tree.map(item => item.adviceCode)
                                fn(res, list)
                            }
                        }
                    })
                }
            },
            del(){
                if(this.groupChecked.length){
                    this.delLoading = true
                    medicalDelete({
                        medicalCode: this.groupChecked[0],
                        checkInMark: this.pInfo.checkInMark,
                    }).then(res => {
                        this.delLoading = false
                        this.$emit('change')
                    }).catch(err => {
                        this.delLoading = false
                    })
                }else{
                    this.$message.error('请选择组')
                }
            },
            stop(){
                if(this.groupChecked.length){
                    this.stopLoading = true
                    medicalApplyterm({
                        adviceCode: this.groupChecked[0],
                        checkInMark: this.pInfo.checkInMark,
                        doctor: localStorage.userDoctorNumber,
                        institutionNumber: localStorage.institutionNumber,
                    }).then(res => {
                        this.stopLoading = false
                        this.$emit('change')
                    }).catch(err => {
                        this.stopLoading = false
                    })
                }else{
                    this.$message.error('请选择组')
                }
            },
            handleSelectionChange(rows){
                this.rows = rows
            },
            getPrescription(){
                this.prescriptionLoading = true
                this.queryForm
                setTimeout(() => {
                    this.templateList = []
                    this.prescriptionLoading = false
                }, 300)
            },
            print(size){
                let noSubmitList = this.tableData.filter(item => item.changed)
                if(noSubmitList.length){
                    this.$message.error('存在未保存的项目，请先保存再打印')
                    return false
                }
                this.$print(this.$refs.print, { size })
            }
        },
        watch: {
            tableData: {
                handler(list){
                    if(!this.config.noAdd){
                        this.checkArr(list)
                    }
                    setTimeout(() => {
                        let arr = list.filter(item => item.medicalAdviceType)
                        let tree = mapLoction(arr, 'adviceCode', 'items')
                        this.printData = tree
                        this.$emit('input', arr)
                    }, 0)
                },
                deep: true
            }
        },
        beforeDestroy(){
            
        }
    };
</script>
<style scoped>

    .advice >>> .el-input{width: 100%;}
    .advice >>> .el-input .el-input__inner{padding: 0 4px;height: 20px !important;line-height: 20px;border-color: rgba(0,0,0,0);background: none;transition: all 0.4s;}
    .advice >>> .el-input .el-input__inner:hover{border-color: rgba(0,0,0,0.2);}
    .advice >>> .el-input .el-input__inner:focus{background: #fff;border-color: #1890ff;}
    .advice >>> .cell{padding-left: 5px;padding-right: 5px;}
    .advice >>> .cell>span{margin-left: 5px;}
    .advice >>> .is-center .cell{padding-left: 0;padding-right: 0;}
    .advice >>> thead .cell{padding-left: 9px;padding-right: 5px;}

    .advice >>> .el-textarea{width: 100%;}
    .advice >>> .el-textarea__inner{padding: 0 4px;height: 20px;line-height: 20px;border-color: rgba(0,0,0,0);background: none;transition: all 0.4s;resize: none;}
    .advice >>> .el-textarea__inner:focus{background: #fff;border-color: #1890ff;}
    .advice >>> .el-table.tb-edit .cell{padding-top: 0;padding-bottom: 0;}
    .advice >>> .el-table.tb-edit td{padding: 0;height: 30px;}
    .advice >>> .el-input .el-input__inner {cursor: auto;color: #000;}
    .advice >>> .el-input.is-disabled .el-input__inner {cursor: auto;color: #000;border-color: rgba(0,0,0,0) !important;}
    .advice >>> .el-textarea .el-textarea__inner {cursor: auto;color: #000;}
    .advice >>> .el-textarea.is-disabled .el-textarea__inner {cursor: auto;color: #000;}
    .advice >>> .el-input__suffix{display: none;}

    .advice >>> .el-checkbox.is-disabled .el-checkbox__input{cursor: auto;}
    .advice >>> .el-checkbox.opacity .el-checkbox__input{cursor: auto;opacity: 0;}
    .advice >>> .el-checkbox.is-disabled .el-checkbox__inner{cursor: auto;}

    .advice >>> .el-table .colorfalse {background: #fff;}
    .advice >>> .el-table .colortrue {background: #fdf6ec;}

    .btnAll{padding-top: 20px;}
    
    .advice >>> .el-input-number{width: auto;}
    .advice >>> .el-date-editor .el-input__icon{display: none;}
    span.text{vertical-align: middle; font-family: Microsoft Yahei;font-size: 12px;}

    .advice >>> .el-input-number.max input{color: #f66c6c;}


    .advice >>> .el-table__row{background: #fff}
    .advice >>> .el-table__row:hover > td{background: none !important;}
    .advice >>> .el-table--enable-row-hover .el-table__body tr:hover > td{background: #fff;}
    /*.advice >>> td.el-table_1_column_2:hover{background: #eee !important;}*/

    .buttonAll{margin-bottom: 10px;}
    .buttonAll >>> .el-button{margin-right: 10px;margin-left: 0;margin-bottom: 5px;}

    .text >>> .el-checkbox{border:1px solid rgba(0,0,0,0);box-sizing: border-box;position: absolute;left: 0;top: 0;width: 100%;height: 100%;transition: all 0.4s;}
    .text >>> .el-checkbox span{display: none;}
    .text >>> .el-checkbox:hover{border-color: #aaa;}
    .text >>> .el-checkbox.is-checked{border-color: #1890ff;}
    .text >>> .el-checkbox.is-disabled:hover{border-color: rgba(0,0,0,0);cursor: auto;}

    .tableBox{}

    .mytables{display: none;padding: 1px;}
    .mytables table td, .mytables table th{border:1px solid #000;text-align: center;line-height: 24px;padding: 3px 0;color: #000;}
    .mytables table th{font-weight: 600;padding: 6px 0;}
    .mytables h3{text-align: center;font-size: 20px;line-height: 20px;margin: 0;}
    .mytables .info{margin: 24px 0 18px;text-align: center;}
    .mytables .info .item{display: inline-block;margin-right: 20px;}
    .mytables .info .item span{display: inline-block;min-width: 70px;border-bottom: 1px solid #000;text-align-last: left;padding: 0 10px;}
    tr.head td{border:0;padding: 0;}
</style>